#!/bin/bash

#shell_pwd=`cd "$( dirname "$0" )" && pwd`
cluster_id=$1
log_path=$2
start_time=$3
end_time=$4
file_postfix=$(date +'%Y%m%d-%H%M')

GREEN="\E[1;32m"
#YELLOW="\E[1;33m"
RES="\E[0m"

usage (){
  echo -e "${GREEN}Usage:${RES}"
  echo "       ./get-cluster-log.sh <cluster_id/router_id> <log_path> [start_time] [end_time]"
  echo ""
  echo -e "${GREEN}eg:${RES}"
  echo "If you just want many of logs & filter some string, please set log_path like:"
  echo "       ./get-cluster-log.sh rtr-test '/data/mongodb/mongod.log|ipsec|systemd,/var/log/syslog|ipsec'"
  echo ""
  echo -e "${GREEN}Help log_path:${RES}"
  echo "       system - /var/log/kern.log"
  echo "                /var/log/syslog"
  echo "       ipsec - /var/log/auth.log"
  echo "               /var/log/ipsec.log"
  echo "       mongodb - /data/mongodb/mongod.log"
  echo "       mysql - /data/logs/checkservice.log"
}

get_cluster_node (){
  webservice_host=$(grep "webservice" /etc/hosts|head -n 1|awk '{print $3}')
  python_env="export PYTHONPATH=/pitrix/lib/pitrix-common:/pitrix/lib/pitrix-cli:\${PYTHONPATH};"
  node_cli="/pitrix/cli/describe-cluster-nodes"
  cli_conf="/pitrix/conf/client.yaml"
  hyper_zone=($(ssh -Tq $webservice_host "${python_env}${node_cli} -c ${cluster_id} -f ${cli_conf}"|grep '"host_machine"'|awk -F'\"' '{print $4}'))
  vm_zone=($(ssh -Tq $webservice_host "${python_env}${node_cli} -c ${cluster_id} -f ${cli_conf}"|grep '"instance_id"'|awk -F'\"' '{print $4}'))
  node_num=$(ssh -Tq $webservice_host "${python_env}${node_cli} -c ${cluster_id} -f ${cli_conf}"|grep '"host_machine"'|wc -l)
}

get_vpc_node (){
  exec_sql="/pitrix/bin/exec_sql.py"
  get_vpc_sql="select a.instance_id,b.host_machine from router as a,instance as b where a.router_id='${cluster_id}' and a.instance_id=b.instance_id;"
  vpc_results=$(${exec_sql} -d zone -c "${get_vpc_sql}" 2>/dev/null|grep 'i-')
  hyper_zone=($(echo ${vpc_results}|awk '{print $4}'))
  vm_zone=($(echo ${vpc_results}|awk '{print $2}'))
  node_num=1
}

get_qingstor_url (){
  if [[ $(grep "pek3b" /etc/hosts|wc -l) != 0 ]]
  then
    zone_id="pek3b"
    qingstor_url="https://newton-file.pek3b.qingstor.com"
    bucket="newton-file"
  elif [[ $(grep "pek3a" /etc/hosts|wc -l) != 0 ]]
  then
    zone_id="pek3a"
    qingstor_url="https://newton-file-pek3a.pek3a.qingstor.com"
    bucket="newton-file-pek3a"
  elif [[ $(grep "sh1a" /etc/hosts|wc -l) != 0 ]]
  then
    zone_id="sh1a"
    qingstor_url="https://newton-file-sh.sh1a.qingstor.com"
    bucket="newton-file-sh"
  elif [[ $(grep "gd2" /etc/hosts|wc -l) != 0 ]]
  then
    zone_id="gd2"
    qingstor_url="https://newton-file-gd2.gd2.qingstor.com"
    bucket="newton-file-gd2"
  else
    echo "This region is not support!"
    exit 1
  fi
}

check_space (){
  check_hyper=$1
  check_vm=$2
  check_file=$3
  free_space=$(ssh -Tq ${check_hyper} "safe-guest-sh ${check_vm} 'df -k /root'"|grep /|awk '{print $4}')
  file_size=$(ssh -Tq ${check_hyper} "safe-guest-sh ${check_vm} 'du -k ${check_file}'"|awk '{print $1}')
  if [[ ${file_size} -gt ${free_space} ]];
  then
    echo 1
  else
    echo 0
  fi
}

push_log (){
  for ((times=0;times<${node_num};times++));
  do
    origin_log=$(echo $2|awk -F'|' '{print $1}')
    hyper_id=${hyper_zone[$times]}
    vm_id=${vm_zone[$times]}
    ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'ls -l ${origin_log} 2>/dev/null'" >/dev/null
    if [[ $? != 0 ]];then echo "${vm_id}: ${origin_log} is not exist, check again!";continue;fi
    instance_ip=$(ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} '/sbin/ip a'"|grep -A 2 -E 'eth0:|eth0@'|grep 'inet'|awk -F'[/ ]+' '{print $3}')
    init_log_name=$(echo ${origin_log}|awk -F'/' '{print $NF}')
    log_name="${instance_ip}_${file_postfix}_${init_log_name}"
    ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'wget -P /root/ ${qingstor_url}/qsctl;wget -P /root/ ${qingstor_url}/config.yaml'" >/dev/null
    ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'chmod +x /root/qsctl'" >/dev/null
    if [[ $1 == 0 ]]
    then
      log_size=$(ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'ls -l ${origin_log}'"|awk '{print $5}')
      if [[ $(echo ${2}|awk -F'|' '{$1="";print $0}') == "" ]]
      then
        ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} '/root/qsctl -z ${zone_id} -c /root/config.yaml cp ${origin_log} qs://${bucket}/logs/${log_name}' 600" >/dev/null
        echo -e "${GREEN}Download URL:${RES} ${qingstor_url}/logs/${log_name}"
      else
        if [[ $log_size -gt 209715200 ]]
        then
          echo "size is too large, cannot filter string !"
          return 1
        else
          [ $(check_space ${hyper_id} ${vm_id} ${origin_log}) == 0 ] || {
            echo "${vm_id} /root donot have enough space, cannot split or filter !"
            exit 1
          }
          filter_log_cmd="cat ${origin_log}"
          for grep_cmd in $(echo ${2}|awk -F'|' '{$1="";print $0}')
          do
            filter_log_cmd=${filter_log_cmd}"|grep -i ${grep_cmd}"
          done
          ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} '${filter_log_cmd} >>/root/${log_name}'" >/dev/null
          count_log=$(ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'cat /root/${log_name} 2>/dev/null|wc -l' 60")
          if [[ ${count_log} == 0 ]]
          then
            echo "[WARNING]: (${filter_log_cmd}) has no content!"
            continue
          elif [[ ${count_log} -le 50 ]]
          then
            ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'cat /root/${log_name} 2>/dev/null' 60"
          else
            echo "The log result count [${count_log}] is too long, you can view it the link below."
          fi
          ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} '/root/qsctl -z ${zone_id} -c /root/config.yaml cp /root/${log_name} qs://${bucket}/logs/${log_name}' 600" >/dev/null
          echo -e "${GREEN}Download URL:${RES} ${qingstor_url}/logs/${log_name}"
        fi
      fi
    elif [[ $1 == 1 ]]
    then
      [ $(check_space ${hyper_id} ${vm_id} ${origin_log}) == 0 ] || {
        echo "${vm_id} /root donot have enough space, cannot split or filter !"
        exit 1
      }
      if [[ $(echo ${2}|awk -F'|' '{$1="";print $0}') != "" ]]
      then
        for grep_cmd in $(echo ${2}|awk -F'|' '{$1="";print $0}')
        do
          filter_log_cmd=${filter_log_cmd}"|grep -i ${grep_cmd}"
        done
      fi
      log_size=$(ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'ls -l ${origin_log}'"|awk '{print $5}')
      if [[ $log_size -gt 209715200 ]]
      then
        ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'split -d -b 200m ${origin_log} /root/split_log_' 600" >/dev/null
        if [[ $? != 0 ]];then echo "[ERROR]: split log [split -d -b 200m ${origin_log} /root/split_log_] failed!";fi
        for log_split in $(ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'ls /root/split_log_*'")
        do
          date_exist=$(ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} \"grep -E '$3|$4' ${log_split}|wc -l\" 60")
          if [[ $date_exist != 0 ]]
          then
            ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} \"sed -n '/${3}/,/${4}/ p' ${log_split}${filter_log_cmd} >>/root/${log_name}\" 300" >/dev/null
          fi
        done
      else
        ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} \"sed -n '/${3}/,/${4}/ p' ${origin_log}${filter_log_cmd} >>/root/${log_name}\" 300" >/dev/null
      fi
      count_log=$(ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'cat /root/${log_name} 2>/dev/null|wc -l' 60")
      if [[ ${count_log} == 0 ]]
      then
        echo "[WARNING]: $vm_id in ${3}-${4} has no content!"
        continue
      elif [[ ${count_log} -le 50 ]]
      then
        ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'cat /root/${log_name} 2>/dev/null' 60"
      else
        echo "The log result count [${count_log}] is too long, you can view it the link below."
      fi
      ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} '/root/qsctl -z ${zone_id} -c /root/config.yaml cp /root/${log_name} qs://${bucket}/logs/${log_name}' 300" >/dev/null
      echo -e "${GREEN}Download URL:${RES} ${qingstor_url}/logs/${log_name}"
    fi
    ssh -Tq ${hyper_id} "safe-guest-sh ${vm_id} 'rm -f /root/qsctl /root/config.yaml /root/${log_name} /root/split_log_*' 60" >/dev/null
  done
}

if [[ $cluster_id == "" ]] || [[ $log_path == "" ]];
then
  usage
  exit 1
fi

if [[ $(echo ${cluster_id}|grep "cl-") != "" ]];
then
  get_cluster_node
elif [[ $(echo ${cluster_id}|grep "rtr-") != "" ]];
then
  get_vpc_node
else
  echo "This resouce type is not support!"
  exit 1
fi
get_qingstor_url

if [[ $start_time != "" ]] && [[ $end_time != "" ]]
then
  for log in ${log_path//,/ };
  do
    push_log 1 ${log} $start_time $end_time
  done
elif [[ $start_time == "" ]] && [[ $end_time == "" ]]
then
  for log in ${log_path//,/ };
  do
    push_log 0 ${log}
  done
else
  echo "Parameters start_time and end_time must be set up together!"
  usage
  exit 1
fi
